package com.sky.directdeal;

import java.util.HashMap;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.directdeal.manager.SharedPreferenceManager;
import com.sky.directdeal.utils.SHA256FileNameGenerator;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.register_app_details.RegisterAppDetailsResponse;

public class DDApplication extends Application {
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Utils.globalConfigurationIntitalize(this);
		initImageLoader(getApplicationContext());
		registerGcm();
	}
	private void registerGcm() {
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		String regId = GCMRegistrar.getRegistrationId(this);

		if (TextUtils.isEmpty(regId)) {
			// Registration is not present, register now with GCM
			GCMRegistrar.register(this, Constants.SENDER_ID);
			regId = GCMRegistrar.getRegistrationId(this);
			Log.e("gcm id", "id "+regId);
			
		} else {
			// Device is already registered on GCM

			if (GCMRegistrar.isRegisteredOnServer(this)) {
				// Skips registration.
				// Toast.makeText(getApplicationContext(),
				// "Already registered with GCM", Toast.LENGTH_LONG).show();
			}
		}
		if (regId != null) {
				registerGCM(regId);
			
		}
	}
	
	private void registerGCM(String gcmId){
		String deviceId=Utils.getAndroidSecureId(getApplicationContext());
		int versionCode=Utils.getAppVersionCode(getApplicationContext());
		Log.e("-----------dev and gcm id ", " dev "+deviceId+" gcm "+versionCode);
		HashMap<String, String> params=new HashMap<String, String>();
		params.put("device_id", deviceId);
		params.put("gcm_id", gcmId);
		params.put("version_code", String.valueOf(versionCode));
		ServiceManager.putAppDetails(getApplicationContext(), params, appDetailsListener);
	}
	
	private ServiceResponseListener<RegisterAppDetailsResponse> appDetailsListener=new ServiceResponseListener<RegisterAppDetailsResponse>() {
		
		@Override
		public void onSuccess(RegisterAppDetailsResponse response) {
			if(response!=null){
				int updateStatus=response.getUpdateStatus();
				SharedPreferenceManager.savePreferences(getApplicationContext(), SharedPreferenceManager.Keys.UPDATE_TYPE, updateStatus);
			}
		}
		
		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			// TODO Auto-generated method stub
			
		}
	};
	
	public static void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
		.threadPriority(Thread.NORM_PRIORITY - 2)
		.denyCacheImageMultipleSizesInMemory()
		.discCacheFileNameGenerator(new SHA256FileNameGenerator())
		.tasksProcessingOrder(QueueProcessingType.LIFO)
		.build();
		ImageLoader.getInstance().init(config);
	}
}
